package com.lh.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lh.api.dao.UserAddressMapper;
import com.lh.api.dao.UserMapper;
import com.lh.api.form.LoginForm;
import com.lh.api.service.UserService;
import com.lh.api.vo.UserAddrVo;
import com.lh.common.entity.admin.User;
import com.lh.common.utils.AssertUtil;
import com.lh.common.utils.Constant;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

	@Autowired
	private UserAddressMapper userAddressMapper;

	@Override
	public User queryByMobile(String mobile) {
		return baseMapper.selectOne(new QueryWrapper<User>().eq("phone", mobile).eq("type","1"));
	}

	@Override
	public User login(LoginForm form) {
		User user = queryByMobile(form.getMobile());
		AssertUtil.assertNotNull(user, Constant.RESULT.FI1006, "Mobile No. and password do not match");

		//密码错误
		if(!user.getPassword().equals(DigestUtils.sha256Hex(form.getPassword()))){
			return null;
		}
		return user;
	}

	@Override
	public int updateByKey(User user) {
		return baseMapper.updateByKey(user);
	}

	@Override
	public UserAddrVo getDefaultAddress(Long uid) {
		return userAddressMapper.getDefaultAddress(uid);
	}

	@Override
	public void invalidRobot(String phone) {
		baseMapper.invalidRobot(phone);
	}
}
